<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-if="!isFollowed"
    @click="onFollow"
    :loading="isBtnLoadingShow"
  >
    关注
  </van-button>
  <van-button
    v-else
    class="follow-btn"
    round
    @click="onFollow"
    size="small"
    :loading="isBtnLoadingShow"
  >
    已关注
  </van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'

export default {
  name: 'follow-user',
  data() {
    return {
      isBtnLoadingShow: false,
    }
  },
  model: {
    prop: 'isFollowed',
    event: 'update-follow'
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, String],
      required: true
    }
  },
  methods: {
    async onFollow () {
      this.isBtnLoadingShow = true
      try {
        if (this.isFollowed) {
          await deleteFollow(this.autId)
        } else {
          await addFollow(this.autId)
        }
        this.$emit('update-follow', !this.isFollowed)
      } catch(err) {
        if ( err.response && err.response.status === 400) {
          this.$toast('你不能关注自己')
        } else {
          this.$toast('关注失败')
        }
      }
      this.isBtnLoadingShow = false
    }
  }
}
</script>

<style>

</style>
